<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>配置商户通道</title>
	<#include "lib/lib.html" />
</head>
<body>
	<form action="" method="post" class="form form-horizontal" id="form-company-add">
	<input type="hidden" name="id" value="${map.id}"/>
	<div class="pop_main" style="padding-left:30px;width: 95%;">
		<div class="panel panel-primary mt-20">
	    	<div class="panel-header">基础配置</div>
	    	<div class="panel-body">
	    		<table class="table table-border table-bordered table-hover table-bg table-sort">
					<tbody>
	    		    	<tr>
	    		    		<td style="width:120px;"><strong>通道路由策略：</strong></td>
	    		    		<td>
	    		    			<select style="width:100px;" class="select size-M" id="isRandom" name="isRandom" size="1">
									<option value="1" <#if obj.isRandom==1>selected="selected"</#if>>随机</option>
									<option value="2" <#if obj.isRandom==2>selected="selected"</#if>>排序</option>
									<option value="3" <#if obj.isRandom==3>selected="selected"</#if>>循环</option>
								</select>
	    		    		</td>
	    		    	</tr>
    		    	</tbody>
			    </table>
	    	</div>
    	</div>
		<div class="panel panel-primary mt-20">
	    	<div class="panel-header">支付通道</div>
	    	<div class="panel-body">
	    		<table class="table table-border table-bordered table-hover table-bg table-sort">
					<tbody>
	    		    	<tr>
	    		    		<td style="width:120px;"><strong>通道路由随机：</strong></td>
	    		    		<td>
	    		    			<select style="width:100px;" class="select size-M" id="isRandom" name="isRandom" size="1">
									<option value="1" <#if obj.isRandom==1>selected="selected"</#if>>随机</option>
									<option value="2" <#if obj.isRandom==2>selected="selected"</#if>>排序</option>
									<option value="3" <#if obj.isRandom==3>selected="selected"</#if>>循环</option>
								</select>
	    		    		</td>
	    		    	</tr>
	    		    	<tr>
	    		    		<td style="width:120px;"><strong>统一设置限额：</strong></td>
	    		    		<td>
	    		    			<input style="width:65px;" class="dd_input input-text size-M" type="text" id="plDay" placeholder="日限额" onchange="ValidateFloat($(this),value)">
		    		    		<button type="button" onclick="plSetting(1)" class="btn btn-success radius">设置日限额</button> | 
		    		    		<input style="width:65px;" class="dd_input input-text size-M" type="text" id="plMin" placeholder="单笔下限" onchange="ValidateFloat($(this),value)">
		    		    		<button type="button" onclick="plSetting(2)" class="btn btn-success radius">单笔下限</button> | 
		    		    		<input style="width:65px;" class="dd_input input-text size-M" type="text" id="plMax" placeholder="单笔上限" onchange="ValidateFloat($(this),value)">
		    		    		<button type="button" onclick="plSetting(3)" class="btn btn-success radius">单笔上限</button>
		    		    	</td>
	    		    	</tr>
	    		    	<tr>
	    		    		<td style="width:120px;"><strong>支付通道配置：</strong></td>
	    		    		<td>
	    		    			<div id="chooseCity" style="font-size:0px;">
	    		    			<#if wayList?? && wayList?size gt 0>
								<#list wayList as dl >
									<#if dl_index ==0>
										<span id="payWaySpan">
										<!-- <input type="button" style="width:50px;height:30px;"  onclick="add()" value="创建">
										<input type="button" style="width:50px;height:30px;"  onclick="submitAdd()" value="提交"></br> -->
										<select style="width:100px;" class="select" name="discoutShopRangeProvince" id="search_shopRangeProvince" onchange="changePayType(this)">
											<option value="0">请选择</option>
											<#if payList?? && payList?size gt 0>
												<#list payList as dl1 >
													<option value="${dl1.id}" <#if dl1.id == dl.payWayId>selected="selected"</#if>>${dl1.wayName}</option>
												</#list>
											</#if>
										</select><span id ="span">
											<#if dl.rateId!>
												<select style="width:180px;" class="select" name="discoutShopRangeCity" id="search_shopRangeCity" style="width:150px;">
													<#if dl.rateList?? && dl.rateList?size gt 0>
														<#list dl.rateList as dl1>
															<option value="${dl1.id}" <#if dl1.id == dl.rateId>selected="selected"</#if>>${dl1.rateName}</option>
														</#list>
													</#if>
												</select>
											</#if>
										</span>
										<b>费率</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="payWayRate" id="payWayRate" value ="${dl.payWayRate}">
										<b>‰，排序</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="sortNum" id="sortNum" value ="${dl.sortNum}"  onchange="ValidateFloat($(this),value)"">
										<b>，每日限</b><input style="width:65px;" class="dd_input input-text size-M" type="text" name="maxMoney" id="maxMoney" value ="${dl.maxMoney}" onchange="ValidateFloat($(this),value)">
										<b>元，单笔限</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMinMoney" id="oneMinMoney" value ="${dl.oneMinMoney}" onchange="ValidateFloat($(this),value)">
										<b>＜金额≤</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMaxMoney" id="oneMaxMoney" value ="${dl.oneMaxMoney}" onchange="ValidateFloat($(this),value)"><b>元&nbsp;&nbsp;</b>
										<button type="button" onclick="addPayWay()" class="btn btn-success radius">添加</button>
										</span>
									<#else>
									<span id="payWaySpan">
										<br/>
										<select style="width:100px;" class="select" name="discoutShopRangeProvince" id="search_shopRangeProvince" onchange="changePayType(this)">
											<option value="0">请选择</option>
											<#if payList?? && payList?size gt 0>
												<#list payList as dl1 >
													<option value="${dl1.id}" <#if dl1.id == dl.payWayId>selected="selected"</#if>>${dl1.wayName}</option>
												</#list>
											</#if>
										</select>
										<span id ="span">
											<#if dl.rateId!>
												<select style="width:180px;" class="select" name="discoutShopRangeCity" id="search_shopRangeCity">
													<#if dl.rateList?? && dl.rateList?size gt 0>
														<#list dl.rateList as dl1>
															<option value="${dl1.id}" <#if dl1.id == dl.rateId>selected="selected"</#if>>${dl1.rateName}</option>
														</#list>
													</#if>
												</select>
											</#if>
										</span>
										<b>费率</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="payWayRate" id="payWayRate" value ="${dl.payWayRate}">
										<b>‰，排序</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="sortNum" id="sortNum" value ="${dl.sortNum}"  onchange="ValidateFloat($(this),value)"">
										<b>，每日限</b><input style="width:65px;" class="dd_input input-text size-M" type="text" name="maxMoney" id="maxMoney" value ="${dl.maxMoney}" onchange="ValidateFloat($(this),value)">
										<b>元，单笔限</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMinMoney" id="oneMinMoney" value ="${dl.oneMinMoney}" onchange="ValidateFloat($(this),value)">
										<b>＜金额≤</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMaxMoney" id="oneMaxMoney" value ="${dl.oneMaxMoney}" onchange="ValidateFloat($(this),value)"><b>元&nbsp;&nbsp;</b>
										<button type="button"  onclick="deleteLabel(this)" class="btn btn-danger radius">删除</button>
									</span>
									</#if>
								</#list>
								<!-- 如果没有数据的时候 显示添加-->
								<#else>
								<span>
									<select style="width:100px;" class="select" name="discoutShopRangeProvince" id="search_shopRangeProvince" onchange="changePayType(this)">
										<option value="0">请选择</option>
										<#if payList?? && payList?size gt 0>
											<#list payList as dl >
												<option value="${dl.id}">${dl.wayName}</option>
											</#list>
										</#if>
									</select>
									<span id ="span">
										<select style="width:180px;" class="select" name="discoutShopRangeCity" id="search_shopRangeCity" style="width:150px;"><option value="0">请选择</option></select>
									</span>
									<b>费率</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="payWayRate" id="payWayRate">
									<b>‰，排序</b><input style="width:45px;" class="dd_input input-text size-M" type="text" name="sortNum" id="sortNum"  onchange="ValidateFloat($(this),value)">
									<b>，每日限</b><input style="width:65px;" class="dd_input input-text size-M" type="text" name="maxMoney" id="maxMoney" onchange="ValidateFloat($(this),value)">
									<b>元，单笔限</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMinMoney" id="oneMinMoney" value ="${dl.oneMinMoney}" onchange="ValidateFloat($(this),value)">
									<b>＜金额≤</b><input style="width:50px;" class="dd_input input-text size-M" type="text" name="oneMaxMoney" id="oneMaxMoney" value ="${dl.oneMaxMoney}" onchange="ValidateFloat($(this),value)"><b>元&nbsp;&nbsp;</b>
									<button type="button" onclick="addPayWay()" class="btn btn-success radius">添加</button>
								</span>
								</#if>
								</div>
	    		    		</td>
	    		    	</tr>
    		    	</tbody>
			    </table>
	    	</div>
    	</div>
		<input type="hidden" name="payWayList" value="${payWayList}" id="shopRangeCity">
		<#if map.type!=1>
			<div class="submit_btn">
				<button style="width:100px;height:40px;" type="button" name="" value="提    交" onclick="submitUpdateCompanyInfo();" class="btn btn-warning radius"><i class="Hui-iconfont">&#xe676;</i> 提    交</button>
			</div>
		</#if>
	</div>
	</form>
	<script type="text/javascript">
		var isChange=0;
		$(document).ready(function(){
			$('select').searchableSelect();
			isChange++;
		});
		
		$(function(){
			$("#form-company-add").Validform({
				tiptype:3,
				showAllError:true,   //表单提交时显示所有提示信息
				ignoreHidden: true,  //隐藏的表单元素不校验
				ajaxPost:true,
				callback:function(data){
					$.Hidemsg();
					if(data.resultCode==200){
						layer.msg('保存成功!', {
							icon : 1,
							time : 1000
						});
						//parent.location.replace("${basePath}/payv2PayWayRate/payv2PayWayRateListAll.do?payWayId="+$("#payViewType").val());
						setTimeout(function () { 
					        layer_close();
					    }, 1000);
					}else{
						layer.msg('保存失败，请你检查数据!', {
							icon : 1,
							time : 1000
						});
					}
				}
			});
		});
	
		function plSetting(idx){
			var vl = "";
			if(idx == 1) {
				vl = $("#plDay").val();
			} else if(idx == 2){
				vl = $("#plMin").val();
			} else if(idx == 3){
				vl = $("#plMax").val();
			}
			if(vl == "") {
				layer.msg("请输入数字");
				return;
			}
			for(var i = 0, len = $("#chooseCity").children().length;i<len;i++){
				if(idx == 1) {
					$("#chooseCity").children().eq(i).find("#maxMoney").val(vl);
				} else if(idx == 2){
					$("#chooseCity").children().eq(i).find("#oneMinMoney").val(vl);
				} else if(idx == 3){
					$("#chooseCity").children().eq(i).find("#oneMaxMoney").val(vl);
				}
			}
		}
		
		function sortNumList(list) {
			var result = 1;
			var temp = "";
			for (var i = 0; i < list.length; i++) {
				var int = list[i];
				if(temp == "") {
					temp = int;
					continue;
				}
				if(int > temp) {
					result = int;
				}else{
					result = temp;
				}
			}
			return result;
		}
		
		function addPayWay(){
			var sortList = new Array();
			for(var i = 0, len = $("#chooseCity").children().length;i<len;i++){
  	            var sortNum = $("#chooseCity").children().eq(i).find("#sortNum").val();
  	            if(sortNum != "") {
  	            	sortList.push(sortNum);
  	            }
			}
			var number = 1;
			if(sortList.length > 0) {
				number = sortNumList(sortList);
				number++;
			}
			
			var html = "<span id=\"payWaySpan\"><br/>"+
				"<select style=\"width:100px;\" class=\"select\" name=\"discoutShopRangeProvince\" id=\"search_shopRangeProvince\" onchange=\"changePayType(this)\">"+
				$("#search_shopRangeProvince").html().replace('selected="selected"','')+
				"</select>"+
				"<span id=\"span\"><select style=\"width:180px;\" class=\"select\" name=\"discoutShopRangeCity\" id=\"search_shopRangeCity\" style=\"width:150px;\"><option value=\"0\">请选择</option></select>"+
				"</span>"+
				"<b>费率</b><input style=\"width:45px;\" class=\"dd_input input-text size-M\" type=\"text\" name=\"payWayRate\" id=\"payWayRate\">"
				+"<b>‰，排序</b><input style=\"width:45px;\" class=\"dd_input input-text size-M\" type=\"text\" name=\"sortNum\" id=\"sortNum\" value=\""+number+"\" onchange=\"ValidateFloat($(this),value)\"\">"
				+"<b>，每日限</b><input style=\"width:65px;\" class=\"dd_input input-text size-M\" type=\"text\" name=\"maxMoney\" id=\"maxMoney\" value=\"200000\" onkeyup=\"if(isNaN(value))execCommand('undo')\" onafterpaste=\"if(isNaN(value))execCommand('undo')\">"
				+"<b>元，单笔限</b><input style=\"width:50px;\" class=\"dd_input input-text size-M\" type=\"text\" name=\"oneMinMoney\" id=\"oneMinMoney\" value =\"0\" onchange=\"ValidateFloat($(this),value)\">"
				+"<b>＜金额≤</b><input style=\"width:50px;\" class=\"dd_input input-text size-M\" type=\"text\" name=\"oneMaxMoney\" id=\"oneMaxMoney\" value =\"0\" onkeyup=\"if(isNaN(value))execCommand('undo')\" onafterpaste=\"if(isNaN(value))execCommand('undo')\"><b>元&nbsp;&nbsp;</b>"
				+"<button type=\"button\"  onclick=\"deleteLabel(this)\" class=\"btn btn-danger radius\">删除</button></span>";
			$("#chooseCity").append(html);
			for(var i = 0, len = $("#chooseCity").children().length;i<len;i++){
				if(i == len-1) {
					$("#chooseCity").children().eq(i).find("#search_shopRangeProvince").searchableSelect();
					//$("#chooseCity").children().eq(i).find("#search_shopRangeCity").searchableSelect();
				}
			}
		}
		
		function changePayType(obj){
			if(isChange == 0)
				return;
			var pro = $(obj).val();
			//alert(isChange+"---"+pro);
			if(pro!="0"){
				var existIdArray = new Array();
				for(var i = 0, len = $("#chooseCity").children().length;i<len;i++){
	  	            var payWayId = $("#chooseCity").children().eq(i).find("#search_shopRangeCity").val();
	  	            if(payWayId != 0) {
	  	            	existIdArray.push(payWayId);
	  	            }
				}
				var existIds = existIdArray.join(",");
				ajaxFunction("/payv2Channel/getPayWayRate.do",{payWayId:pro,existIds:existIds},"JSON",true,function(result){
					if(!!result && result.resultCode == 200){
						var list = result.Data, str="";
						for(var i=0;i<list.length;i++){
							str+='<option value="'+list[i].id+'">'+list[i].rateName+'</option>';
						}
						if(str!=""){
							//str = '<select style="width:180px;" class="select" name="discoutShopRangeCity" id="search_shopRangeCity" style="width:150px;">'+str+'</select>';
							$(obj).parent().find("#search_shopRangeCity").html(str);
							$(obj).parent().find("#span .searchable-select").remove();
							$(obj).parent().find("#span #search_shopRangeCity").searchableSelect();
						}else{
							layer.msg('该支付方式暂无通道', {icon: 2});
							$(obj).parent().find("#search_shopRangeCity").html("<option value=\"0\">请选择</option>");
							$(obj).parent().find("#span .searchable-select").remove();
							$(obj).parent().find("#span #search_shopRangeCity").searchableSelect();
						}
					}else{
						layer.msg(result.message, {icon: 2});
					}
				});
			} else {
				$(obj).parent().find("#search_shopRangeCity").html("<option value=\"0\">请选择</option>");
				$(obj).parent().find("#span .searchable-select").remove();
				$(obj).parent().find("#span #search_shopRangeCity").searchableSelect();
			}
			
		}
		
		function submitUpdateCompanyInfo(){
			var result = "";
			var sortList = new Array();
			var wayList = new Array();
			for(var i = 0, len = $("#chooseCity").children().length;i<len;i++){
				var payTypeId = $("#chooseCity").children().eq(i).find("#search_shopRangeProvince").val();
  	            var payWayId = $("#chooseCity").children().eq(i).find("#search_shopRangeCity").val();
  	            var payWayRate = $("#chooseCity").children().eq(i).find("#payWayRate").val();
  	            var sortNum = $("#chooseCity").children().eq(i).find("#sortNum").val();
  	            var maxMoney = $("#chooseCity").children().eq(i).find("#maxMoney").val();
  	            var oneMinMoney = $("#chooseCity").children().eq(i).find("#oneMinMoney").val();
  	            var oneMaxMoney = $("#chooseCity").children().eq(i).find("#oneMaxMoney").val();
  	            if(payTypeId == "0" || payWayId == "0" || payWayRate == "" || payWayRate == "0" || sortNum == "") {
  	            	layer.msg('通道格式错误！', function(){});
  	            	return false;
  	            }
  	            if(contains(sortList, sortNum)) {
  	            	layer.msg('排序序号重复！', function(){});
  	            	return false;
  	            } else {
  	            	sortList.push(sortNum);
  	            }
  	            if(maxMoney == "") {
  	            	maxMoney = 0;
  	            }
  	            if(oneMaxMoney == "") {
  	            	oneMaxMoney = 0;
  	            }
  	            wayList.push(payTypeId+"-"+payWayId+"-"+payWayRate + "-"+sortNum+"-"+maxMoney+"-"+oneMinMoney+"-"+oneMaxMoney);
			}
	        result = wayList.join(",");
			if(result != ''){
				$("#shopRangeCity").val(result);
			}else{
  	            layer.msg('通道格式错误！', function(){});
				return false;
			}
			var shopRangeCity = $("#shopRangeCity").val();
			if(!!shopRangeCity&&shopRangeCity!=""){
				$("#form-company-add").attr(
						"action",
						"${basePath}/payv2BussCompany/configCompanyWayRate.do").submit(); 
			}else{
				layer.msg('请分配支付通道！', function(){});
			}
		}
		
		function contains(arr, obj) {  
	        var i = arr.length;  
	        while (i--) {  
	            if (arr[i] == obj) {  
	                return true;  
	            }  
	        }  
	        return false;  
	    }
	</script>
	
	
	
	
	
	
	
	
	
	
	
	
	<script type="text/javascript">
	$(function(){
		
		$("#form-company-add").Validform({
			tiptype:3,
			showAllError:true,   //表单提交时显示所有提示信息
			ignoreHidden: true,  //隐藏的表单元素不校验
			ajaxPost:true,
			callback:function(data){
				$.Hidemsg()
				if(data.resultCode==200){
					layer.msg('保存成功!', {
						icon : 1,
						time : 1000
					});
					parent.location.replace("${basePath}/payv2BussCompany/companyList.do");
					layer_close();
				}else{
					//$("#form-company-add").resetForm();
					layer.msg('保存失败，请你检查数据!', {
						icon : 1,
						time : 1000
					});
				}
			}
		});
	});
	
		
		function ValidateFloat(e, pnumber){ 
			if (!/^\d+[.]?\d*$/.test(pnumber)){ 
				$(e).val(/^\d+[.]?\d*/.exec($(e).val())); 
				layer.msg('请输入整数！', function(){});
			}
			return false; 
		}
		//判断密码是否一致
		function ifPassword(e, pnumber){ 
			var p1=$("#passWord").val();
			var p2=$("#passWordTwo").val();
			if (p1!=p2){ 
				layer.msg('两次输入的密码不一致，重新输入！', function(){});
				$(e).val("");
				$("#passWord").val("");
			}
			return false; 
		}
		function deleteLabel(obj){
			$(obj).parent().remove();
			//index--;
		}
		
		//简单验证身份证
		function ValidatelegalIdCard(e, pnumber){
			//身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字
			var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
			if(reg.test(pnumber)==false){
				layer.msg('身份证格式错误！', function(){});
				return false;
			}else{
				return true;
			}
		}
		
		//验证用户名字是否是邮箱或者手机号码
		function checkUserName(e, pnumber){
			var falseOrtrue=false;
			if (!(/^1[34578]\d{9}$/.test(pnumber)||/^0\d{2,3}-?\d{7,8}$/.test(pnumber))){ 
				falseOrtrue=false;
			}else{
				falseOrtrue=true;
				validateUserName(falseOrtrue,pnumber);
				return true;
			}
			//验证是否是邮箱
			var regemail=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if(regemail.test(pnumber)){
				falseOrtrue=true;
				validateUserName(falseOrtrue,pnumber);
				return true;
			}else{
				$(e).val("");
				falseOrtrue=false;
			}
			if(falseOrtrue==false){
				layer.msg('请使用邮箱或手机号作为商户账号！', function(){});
				return false;
			}
		}
		
		function validateUserName(falseOrtrue,pnumber){
			var sourceUserName = $("#sourceUserName").val();
			if(falseOrtrue && pnumber!=sourceUserName){
				$.ajax({
					type:"get",
					url:"${basePath}/payv2BussCompany/validateUserName.do?userName="+pnumber,
					async:false,
					success:function(data){
						if(!!data&&data.resultCode==200){
							
						}else{
							layer.msg(data.message, function(){});
						}
					}
				});
			}
		}
	</script>
</body>
</html>